import { Observable, Subscriber, fromEvent, merge } from 'rxjs'
import { createSocket, SocketType, RemoteInfo, Socket } from 'dgram'
import { Injectable } from '@nestjs/common'
import { createDgramSocket } from '../../../functions'
@Injectable()
export class DgramService {
  // 创建客户端
  createSocket(
    type: SocketType = 'udp4'
  ): Observable<{ server: Socket; msg: Buffer; rinfo: RemoteInfo }> {
    return createDgramSocket(type)
  }
  // 监听消息
  on(server: Socket): Observable<any> {
    const close = fromEvent(server, 'close')
    const error = fromEvent(server, 'error')
    const listening = fromEvent(server, 'listening')
    const message = fromEvent(server, 'message')
    return merge(close, error, listening, message)
  }
}
